package swing_UI;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.util.Date;

import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import java.awt.event.ActionListener;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class ServerGUI {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private AbstractAction analyseTraffic;
	private JTextPane title;
	private JButton jButton1;
	private JButton unitBack;
	private JLabel statisticsLabel;
	private JFrame frame;
	private JPanel jPanel1;
	private JButton exportButton;
	private JScrollPane revenueTable;
	private JTable realRevenueTable;
	private JTable realTrafficTable;
	private JScrollPane trafficTable;
	private JButton yearlyButton;
	private JButton monthlyButton;
	private JButton dailyButton;
	private JPanel jPanel2;
	private JDialog analyseStuff;
	private JMenuItem anTraffic;
	private JMenu analytics;
	
	public ClientButton[] buttons;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		
		
		ServerGUI window = new ServerGUI();
		window.frame.setVisible(true);
		
		ClientManager manager = new ClientManager();
		for(int i = 0; i < window.buttons.length; i++)
		{
			Client c = new Client(i);
			c.init();
			ClientObserver co = new ClientObserver(window.buttons[i]);
			c.addChangeListener(co);
			manager.addClient(i, c);
		}
		
	}

	/**
	 * Create the application.
	 */
	public ServerGUI() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(0, 0, 679, 490);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JMenuBar menuBar = new JMenuBar();
		frame.setJMenuBar(menuBar);
		
		JMenu mnFile = new JMenu("File");
		menuBar.add(mnFile);
		
		JMenuItem mntmExit = new JMenuItem("Exit");
		mnFile.add(mntmExit);
		{
			analytics = new JMenu();
			analytics.setText("Analyze");
			menuBar.add(analytics);
			{
				anTraffic = new JMenuItem();
				analytics.add(anTraffic);
				anTraffic.setText("Revenues");
				anTraffic.setAction(getAnalyseTraffic());
			}
		}
		
		
		
		frame.getContentPane().setLayout(null);
		
		JButton btnRegisterNewClient = new JButton("Register New Client");
		btnRegisterNewClient.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ConnectDialog conn = new ConnectDialog();
				conn.setVisible(true);
			}
		});
		btnRegisterNewClient.setBounds(121, 397, 148, 23);
		frame.getContentPane().add(btnRegisterNewClient);
		frame.setTitle("KapeNet");
		this.buttons = new ClientButton[12];
		for(int i = 0; i < buttons.length; i++)
		{
			
			buttons[i] = new ClientButton("");
			buttons[i].addActionListener(new ClientButtonHandler(this.frame));
			buttons[i].setIcon(new ImageIcon("C:\\projects\\KapeNet\\trunk\\res\\computer.jpg"));
			buttons[i].setSize(75, 75);
			buttons[i].setStatus(Client.Status.DISCONNECTED);
			frame.getContentPane().add(buttons[i]);
			
		}
		
		
		for(int i = 0; i < 5; i++)
		{
			buttons[i].setLocation(0, ((85)*i));
			buttons[i+5].setLocation(85*(i+1), 0);
		}
		
		for(int i = 9; i < buttons.length; i++)
		{
			buttons[i].setLocation(425, (i-8)*85);
			
		}
		
	}

	private AbstractAction getAnalyseTraffic() {
		if(analyseTraffic == null) {
			analyseTraffic = new AbstractAction("Revenues", null) {
				/**
				 * 
				 */
				private static final long serialVersionUID = 6550194311375905557L;

				public void actionPerformed(ActionEvent evt) {
					getAnalyseStuff().pack();
					getAnalyseStuff().setLocationRelativeTo(null);
					getAnalyseStuff().setVisible(true);
				}
			};
		}
		return analyseTraffic;
	}
	
	private JDialog getAnalyseStuff() {
		if(analyseStuff == null) {
			analyseStuff = new JDialog();
			analyseStuff.setPreferredSize(new java.awt.Dimension(500, 400));
			analyseStuff.setTitle("Revenues");
			analyseStuff.getContentPane().setLayout(null);
			analyseStuff.getContentPane().add(getJPanel1(), "Center");
			analyseStuff.getContentPane().add(getJPanel2());
			analyseStuff.setSize(500, 400);
		}
		return analyseStuff;
	}
	
	private JPanel getJPanel1() {
		if(jPanel1 == null) {
			jPanel1 = new JPanel();
			jPanel1.setBounds(110, 0, 374, 362);
			jPanel1.setLayout(null);
			jPanel1.add(getDailyButton());
			jPanel1.add(getMonthlyButton());
			jPanel1.add(getYearlyButton());
			jPanel1.add(getTrafficTable());
			jPanel1.add(getJScrollPane1());
			jPanel1.add(getStatisticsLabel());
			jPanel1.add(getUnitBack());
			jPanel1.add(getJButton1());
			jPanel1.add(getTitle());
		}
		return jPanel1;
	}
	
	private JPanel getJPanel2() {
		if(jPanel2 == null) {
			jPanel2 = new JPanel();
			jPanel2.setBounds(0, 326, 100, 36);
			jPanel2.add(getExportButton());
		}
		return jPanel2;
	}

	private JButton getExportButton() {
		if(exportButton == null) {
			exportButton = new JButton();
			exportButton.setText("Export to..");
			exportButton.setPreferredSize(new java.awt.Dimension(89, 23));
		}
		return exportButton;
	}
	
	private JButton getDailyButton() {
		if(dailyButton == null) {
			dailyButton = new JButton();
			dailyButton.setText("Day");
			dailyButton.setBounds(72, 328, 76, 23);
		}
		return dailyButton;
	}
	
	private JButton getMonthlyButton() {
		if(monthlyButton == null) {
			monthlyButton = new JButton();
			monthlyButton.setText("Month");
			monthlyButton.setBounds(167, 328, 76, 23);
		}
		return monthlyButton;
	}
	
	private JButton getYearlyButton() {
		if(yearlyButton == null) {
			yearlyButton = new JButton();
			yearlyButton.setText("Year");
			yearlyButton.setBounds(263, 328, 76, 23);
		}
		return yearlyButton;
	}
	
	private JScrollPane getTrafficTable() {
		if(trafficTable == null) {
			trafficTable = new JScrollPane();
			trafficTable.setBackground(new java.awt.Color(238,238,238));
			trafficTable.setBounds(72, 207, 252, 76);
			trafficTable.setViewportView(getJTable1());
		}
		return trafficTable;
	}
	
	private JTable getJTable1() {
		if(realTrafficTable == null) {
			TableModel jTable1Model = 
					new DefaultTableModel(
							new String[][] { { "Breakfast", "1.3", "12"}, { "Lunch", "8.9", "42" }, {"Evening","14.3","63"} },
							new String[] { "", "Traffic in GB", "Users" });
			realTrafficTable = new JTable();
			realTrafficTable.setModel(jTable1Model);
			realTrafficTable.setEnabled(false);
			realTrafficTable.setPreferredSize(new java.awt.Dimension(252, 49));
			realTrafficTable.setSize(252, 49);
		}
		return realTrafficTable;
	}
	
	private JScrollPane getJScrollPane1() {
		if(revenueTable == null) {
			revenueTable = new JScrollPane();
			revenueTable.setBackground(new java.awt.Color(238,238,238));
			revenueTable.setBounds(72, 109, 252, 76);
			revenueTable.setViewportView(getJTable2());
		}
		return revenueTable;
	}
	
	private JTable getJTable2() {
		if(realRevenueTable == null) {
			TableModel jTable2Model = 
					new DefaultTableModel(
							new String[][] { { "Breakfast", "165", "12"}, { "Lunch", "560", "42" }, {"Evening","888","63"} },
							new String[] { "", "Income in $", "Users" });
			realRevenueTable = new JTable();
			realRevenueTable.setModel(jTable2Model);
			realRevenueTable.setEnabled(false);
			realRevenueTable.setPreferredSize(new java.awt.Dimension(252, 49));
		}
		return realRevenueTable;
	}
	
	@SuppressWarnings("deprecation")
	private JLabel getStatisticsLabel() {
		if(statisticsLabel == null) {
			Date date = new Date();
			statisticsLabel = new JLabel();
			String text = "Statistics for " + date.toLocaleString();
			statisticsLabel.setText(text);
			statisticsLabel.setBounds(72, 11, 252, 27);
			statisticsLabel.setFont(new java.awt.Font("Calibri",1,14));
		}
		return statisticsLabel;
	}
	
	private JButton getUnitBack() {
		if(unitBack == null) {
			unitBack = new JButton();
			unitBack.setText("<");
			unitBack.setBounds(72, 38, 45, 24);
		}
		return unitBack;
	}
	
	private JButton getJButton1() {
		if(jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setText(">");
			jButton1.setBounds(123, 38, 44, 24);
		}
		return jButton1;
	}
	
	private JTextPane getTitle() {
		if(title == null) {
			title = new JTextPane();
			title.setText("Daily");
			title.setEditable(false);
			title.setBackground(new java.awt.Color(238,238,238));
			title.setEnabled(false);
			title.setFont(new java.awt.Font("Broadway",0,22));
			title.setBounds(72, 73, 252, 28);
		}
		return title;
	}

}
